package paneles;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JSlider;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

public class panelMapaImagen extends JPanel implements MouseWheelListener, ChangeListener{

	private static final long serialVersionUID = 1L;
	private JSlider degreesSlider;
	private ZoomPanel panel;
	private int minimo_zoom = 15;
	
    public panelMapaImagen (){    	
         setLayout(new BorderLayout(0, 0));
         
         JScrollPane scrollPane = new JScrollPane();
         scrollPane.setWheelScrollingEnabled(false);
         add(scrollPane, BorderLayout.CENTER);
         
         JViewport vport = scrollPane.getViewport();
         MouseAdapter ma = new HandScrollListener();
         vport.addMouseMotionListener(ma);
         vport.addMouseListener(ma);
         
         panel = new ZoomPanel(new  ImageIcon(panelMapaImagen.class.getResource("/image/mapa.png")));
         scrollPane.setViewportView(panel);
         panel.setBackground(Color.WHITE);
         setBackground(Color.WHITE);
         degreesSlider = new JSlider(minimo_zoom ,100,minimo_zoom );
         degreesSlider.setMajorTickSpacing(2);
         degreesSlider.setMinorTickSpacing(1);
       //degreesSlider.setPaintLabels(true);
         degreesSlider.setPaintTicks(true);
       //degreesSlider.setPaintTrack(true);
         degreesSlider.addMouseWheelListener(this);   
         degreesSlider.addChangeListener(this);
         
         panel.setScale(degreesSlider.getValue() * .01f, degreesSlider.getValue() * .01f);
         
         add(degreesSlider, BorderLayout.SOUTH);
    }
    
    static class HandScrollListener extends MouseAdapter {
        private final Point pp = new Point();
        @Override public void mouseDragged(MouseEvent e) {
          JViewport vport = (JViewport)e.getSource();
          JComponent label = (JComponent)vport.getView();
          Point cp = e.getPoint();
          Point vp = vport.getViewPosition();
          vp.translate(pp.x-cp.x, pp.y-cp.y);
          label.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
          //vport.setViewPosition(vp);
          pp.setLocation(cp);
        }
        @Override public void mousePressed(MouseEvent e) {
          pp.setLocation(e.getPoint());
        }
      }

    /*Listener rueda*/
	public void mouseWheelMoved(MouseWheelEvent evt) {		
	  Object evento = evt.getSource();	  
	  if (evento == degreesSlider){		
		  if (evt.getWheelRotation() > 0){			   
		      int iNewValue = degreesSlider.getValue() - degreesSlider.getMinorTickSpacing();		            
		      if (iNewValue >= degreesSlider.getMinimum()){		                
		    	     degreesSlider.setValue(iNewValue);
		    	     panel.setScale(iNewValue * .01f, iNewValue * .01f);		    	  
		      }else{		    	  
		             degreesSlider.setValue(minimo_zoom ); //Valor minimo
		             panel.setScale(minimo_zoom  * .01f, minimo_zoom  * .01f);
		      }		      
		   }else{
			   
		       int iNewValue = degreesSlider.getValue() + degreesSlider.getMinorTickSpacing();		       
		       if (iNewValue <= degreesSlider.getMaximum()){		    	   
		             degreesSlider.setValue(iNewValue);
		             panel.setScale(iNewValue * .01f, iNewValue * .01f);		             
		       }else{		    	   
		             degreesSlider.setValue(degreesSlider.getMaximum());
		             panel.setScale(degreesSlider.getMaximum()* .01f, degreesSlider.getMaximum() * .01f);
		       }
		   }	
	  }
	}

	/*Listener clic*/
	public void stateChanged(ChangeEvent e) {
		int val = ((JSlider) e.getSource()).getValue();
        panel.setScale(val * .01f, val * .01f);	
	}
    
    public class ZoomPanel extends JPanel {

    	private static final long serialVersionUID = 1L;
    	private Image img;
        private float scaleX, scaleY;

        public ZoomPanel() {
            scaleX = scaleY = 1;
            setOpaque(false);
        }
        public  ZoomPanel(Image img) {
        	
            this.img = new ImageIcon(img).getImage();
            scaleX = scaleY = 1;
            setOpaque(false);
        }
        public ZoomPanel(ImageIcon icon) {
            this(icon.getImage());
        }
        public void setImage(Image img) {
            this.img = img;
            revalidate();
            repaint();
        }
        
        public void setScale(float scaleX, float scaleY) {
            this.scaleX = scaleX;
            this.scaleY = scaleY;
            revalidate();
            repaint();
        }
        @Override
        public Dimension getPreferredSize() {
            int prefWidth  = (int) (img == null?0:img.getWidth(this) * scaleX);
            int prefHeight = (int) (img == null?0:img.getHeight(this) * scaleY);

            return new Dimension(prefWidth,prefHeight);
        }
        @Override
        public void paintComponent(Graphics g) {
            if(img == null)
                return;

            int w = (int) (img.getWidth(null) * scaleX);
            int h = (int) (img.getHeight(null) * scaleY);

            int x = (getWidth()-w)/2;
            int y = (getHeight()-h)/2;

            g.drawImage(img,x,y,w,h, null);
        }

    }
}
